package vumt.radiocountries;

import java.util.Timer;
import java.util.TimerTask;

import vumt.radiocountries.util.Global;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class PlayerActivity extends Activity implements OnClickListener {
	private Button btnClose, btnPlay, btnRecord, btnCloseAdmob;
	private TextView tvName, tvWeb;
	static ProgressBar prgStreaming = null;
	private SeekBar volumeControl;
	private AudioManager audioManager;
	private ImageView ivSound;
	private LinearLayout reLayout;
	private AdView adView;

	String name = "";
	String web = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player_layout);

		btnClose = (Button) findViewById(R.id.btnClose);
		btnCloseAdmob = (Button) findViewById(R.id.btnCloseAdmob);
		btnPlay = (Button) findViewById(R.id.btnPlay);
		btnRecord = (Button) findViewById(R.id.btnRecord);
		tvName = (TextView) findViewById(R.id.tvName);
		tvWeb = (TextView) findViewById(R.id.tvWeb);
		volumeControl = (SeekBar) findViewById(R.id.volume_control);

		btnClose.setOnClickListener(this);
		btnCloseAdmob.setOnClickListener(this);
		btnPlay.setOnClickListener(this);
		btnRecord.setOnClickListener(this);

		ivSound = (ImageView) findViewById(R.id.ivSound);

		initControls();

		reLayout = (LinearLayout) findViewById(R.id.reLayout);
		// ADMOB
		adView = (AdView) findViewById(R.id.ad);
		adView.loadAd(new AdRequest());

		Bundle bundle = this.getIntent().getExtras();
		name = bundle.getString("name");
		web = bundle.getString("website");
		tvName.setText(name);
		tvWeb.setText(web);

		// Progress Streaming
		prgStreaming = (ProgressBar) findViewById(R.id.prgStreaming);
		prgStreaming.setIndeterminate(true);
		prgStreaming.setIndeterminateDrawable(getResources().getDrawable(
				R.anim.progress_streaming));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
			stopRadio();
			finish();
			break;
		case R.id.btnPlay:
			playRadio();
			break;
		case R.id.btnRecord:
			Toast.makeText(PlayerActivity.this, "Disable", Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.btnCloseAdmob:
			TimerStart();
			break;
		default:
			break;
		}
	}

	private int time = 0;
	private Timer t;

	private void TimerStart() {
		reLayout.setVisibility(View.GONE);
		t = new Timer();
		t.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						time += 1;
						if (time > 10)
							TimerStop();
					}
				});
			}
		}, 0, 1000);
	}

	private void TimerStop() {
		reLayout.setVisibility(View.VISIBLE);
		time = 0;
		t.cancel();
	}

	@Override
	public void onBackPressed() {
		stopRadio();
		super.onBackPressed();
	}

	public static void updateview() {
		prgStreaming.setVisibility(View.GONE);
	}

	public void playRadio() {
		if (!Global.isPlaying) {
			btnPlay.setBackgroundResource(R.drawable.btn_stop);
			Global.isPlaying = true;
			try {
				prgStreaming.setVisibility(View.VISIBLE);
				startService(new Intent(PlayerActivity.this,
						vumt.radiocountries.services.ManagerRadio.class));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else
			stopRadio();
	}

	public void stopRadio() {
		Global.isPlaying = false;
		btnPlay.setBackgroundResource(R.drawable.btn_play);
		if (isServiceRunning())
			stopService(new Intent(PlayerActivity.this,
					vumt.radiocountries.services.ManagerRadio.class));
	}

	private boolean isServiceRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("vumt.radiocountries.services.ManagerRadio"
					.equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	private void initControls() {
		try {
			volumeControl = (SeekBar) findViewById(R.id.volume_control);
			audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			volumeControl.setMax(audioManager
					.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
			volumeControl.setProgress(audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC));

			volumeControl
					.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
						@Override
						public void onStopTrackingTouch(SeekBar arg0) {
						}

						@Override
						public void onStartTrackingTouch(SeekBar arg0) {
						}

						@Override
						public void onProgressChanged(SeekBar arg0,
								int progress, boolean arg2) {
							audioManager.setStreamVolume(
									AudioManager.STREAM_MUSIC, progress, 0);
							if (progress == 0)
								ivSound.setImageResource(R.drawable.image_sound_0);
							else if (progress >= 5 && progress < 10)
								ivSound.setImageResource(R.drawable.image_sound_1);
							else if (progress >= 10 && progress < 15)
								ivSound.setImageResource(R.drawable.image_sound_2);
							else if (progress == 15)
								ivSound.setImageResource(R.drawable.image_sound_3);
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
